Prevent the use of "test" as a crate name. Fixes #2432.
authorTim Erwich <timerwich@gmail.com>
Fri, 4 Mar 2016 19:32:22 +0000 (19:32 +0000)
committerTim Erwich <timerwich@gmail.com>
Tue, 22 Mar 2016 08:45:48 +0000 (09:45 +0100)
src/cargo/ops/cargo_new.rs
tests/test_cargo_init.rs
tests/test_cargo_new.rs

index 41fa01fc11fc575db3a17ae5fa04af8f8af473c2..0f5d8b3518fc28b7ede852f4c403b7e987f5b3f0 100644 (file)
@@ -89,6 +89,12 @@ fn get_name<'a>(path: &'a Path, opts: &'a NewOptions, config: &Config) -> CargoR
 }
 
 fn check_name(name: &str) -> CargoResult<()> {
+    if name == "test" {
+        bail!("The name `{}` cannot be used as a crate name\n\
+               use --name to override crate name",
+               name)
+    }
+
     for c in name.chars() {
         if c.is_alphanumeric() { continue }
         if c == '_' || c == '-' { continue }
index 5a708dad4c48707d1e31fee878153eadc27209b0..9bbae87e7dc439fd9d437b64ed85877055a88c26 100644 (file)
@@ -239,6 +239,20 @@ error = ERROR)));
     assert_that(&foo.join("Cargo.toml"), is_not(existing_file()));
 });
 
+test!(reserved_name {
+    let test = &paths::root().join("test");
+    fs::create_dir_all(&test).unwrap();
+    assert_that(cargo_process("init").cwd(test.clone())
+                                     .env("USER", "foo"),
+                execs().with_status(101).with_stderr(&format!("\
+{error} The name `test` cannot be used as a crate name\n\
+use --name to override crate name
+",
+error = ERROR)));
+
+    assert_that(&test.join("Cargo.toml"), is_not(existing_file()));
+});
+
 test!(git_autodetect {
     fs::create_dir(&paths::root().join(".git")).unwrap();
 
index c3e5db359b40d007c644211c656714eea79ddf2c..9b323fa04694b2db69dc2f3e8aa58b9333c108ba 100644 (file)
@@ -95,6 +95,15 @@ use --name to override crate name",
 error = ERROR)));
 });
 
+test!(reserved_name {
+    assert_that(cargo_process("new").arg("test"),
+                execs().with_status(101)
+                       .with_stderr(&format!("\
+{error} The name `test` cannot be used as a crate name\n\
+use --name to override crate name",
+error = ERROR)));
+});
+
 test!(rust_prefix_stripped {
     assert_that(cargo_process("new").arg("rust-foo").env("USER", "foo"),
                 execs().with_status(0)